#include "commonpage.h"
#include "ui_commonpage.h"
#include "listitembox.h"
#include <QDebug>

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);  
    // 关闭水平滚动条
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    // 按钮点击，发送信号
    connect(ui->playAllBtn, &QPushButton::clicked, this, [=]()
    {
       emit playAll(pageType);
    });
    // 双击
    connect(ui->pageMusicList, &QListWidget::doubleClicked, this, [=](const QModelIndex &index)
    {
        emit playMusicByIndex(this, index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUi(const QString &title, const QString &image)
{
    // 标题
    ui->pageTittle->setText(title);
    // 封面
    ui->musicImageLabel->setPixmap(QPixmap(image));
    ui->musicImageLabel->setScaledContents(true);
}

void CommonPage::setMusicListType(PageType pageType)
{
    this->pageType = pageType;
}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    // 旧内容清空
    musicListOfPage.clear();
    for (auto& music : musicList)  // 自定义类手动支持范围for
    {
        switch(pageType)
        {
        case LOCAL_PAGE:
            musicListOfPage.push_back(music.getMusicId());
            break;
        case LIKE_PAGE:
            if (music.getIsLike())
            {
                musicListOfPage.push_back(music.getMusicId());
            }
            break;
        case HISTORY_PAGE:
            if (music.getIsHistory())
            {
                musicListOfPage.push_back(music.getMusicId());
            }
            break;
        default:
            qDebug() << "暂不支持";
            break;
        }
    }
}

void CommonPage::reFresh(MusicList &musicList)
{
    // ui清空
    ui->pageMusicList->clear();
    // 从musicList中分离出当前页面的所有音乐
    addMusicToMusicPage(musicList);
    // 获取音乐的所有信息
    for (auto musicId : musicListOfPage)
    {
        auto it = musicList.findMusicByMusicId(musicId);
        if (it == nullptr)
        {
            continue;
        }

        ListItemBox* listItemBox = new ListItemBox(ui->pageMusicList);
        listItemBox->setMusicName(it->getMusicName());
        listItemBox->setSinger(it->getSingerName());
        listItemBox->setAlbumName(it->getAlbumName());
        listItemBox->setLikeMusic(it->getIsLike());

        QListWidgetItem* item = new QListWidgetItem(ui->pageMusicList);
            // 需要设置宽高，不然显示不全
        item->setSizeHint(QSize(listItemBox->width(), listItemBox->height()));
        ui->pageMusicList->setItemWidget(item, listItemBox);
        // 连接点击喜欢的信号
        connect(listItemBox, &ListItemBox::setIsLike, this, [=](bool isLike)
        {
            // 通知主类，本地页面，收藏页面，最近播放页面更新喜欢信息
            emit updateLikeMusic(isLike, it->getMusicId());
        });
    }

    // 更新完刷新界面
    repaint();
}

void CommonPage::addMusicToPlayer(MusicList &musicList, QMediaPlaylist *playList)
{
    for (auto& music : musicList)
    {
        switch(pageType)
        {
        case LOCAL_PAGE:
            playList->addMedia(music.getMusicUrl());
            break;
        case LIKE_PAGE:
            if (music.getIsLike())
            {
                playList->addMedia(music.getMusicUrl());
            }
            break;
        case HISTORY_PAGE:
            if (music.getIsHistory())
            {
                playList->addMedia(music.getMusicUrl());
            }
            break;
        default:
            qDebug() << "暂不支持";
            break;
        }
    }
}

const QString &CommonPage::getMusicIdByIndex(int index) const
{
    if (index >= musicListOfPage.size())
    {
        qDebug() << "没有这个歌曲";
        return "";
    }

    return musicListOfPage[index];
}

void CommonPage::setImageLabel(QPixmap pixMap)
{
    ui->musicImageLabel->setPixmap(pixMap);
    ui->musicImageLabel->setScaledContents(true);
}


